<template>
  <div>
    <el-card class="login-form-layout">
      <el-form autoComplete="on" :model="loginForm" :rules="loginRules" ref="ruleFormRef" label-position="left">
        <div style="text-align: center">
          <svg-icon icon-class="login-mall" style="width: 56px; height: 56px; color: #409eff"></svg-icon>
        </div>
        <h2 class="login-title color-main">mall</h2>
        <el-form-item prop="username">
          <el-input name="username" type="text" v-model="loginForm.username" autoComplete="on" placeholder="请输入用户名">
            <template #prefix>
              <svg-icon icon-class="user" class="color-main"></svg-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            name="password"
            :type="pwdType"
            @keyup.enter="handleLogin"
            v-model="loginForm.password"
            autoComplete="on"
            placeholder="请输入密码"
          >
            <template #prefix>
              <svg-icon icon-class="password" class="color-main"></svg-icon>
            </template>
            <template #suffix>
              <svg-icon icon-class="eye" class="color-main" @click="showPwd"></svg-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button style="width: 100%" type="primary" :loading="loading" @click.prevent="handleLogin(ruleFormRef)"> 登录 </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import Cookies from 'js-cookie'
import { isvalidUsername } from '@/utils/validate'
import { useRoute, useRouter } from 'vue-router'
import type { FormInstance } from 'element-plus'
import useUserStore from '@/store/modules/user'

defineOptions({ name: 'LOGIN' })

const router = useRouter()
const route = useRoute()
const store = useUserStore()

const validateUsername = (rule: any, value: any, callback: any) => {
  if (!isvalidUsername(value)) {
    callback(new Error('请输入正确的用户名'))
  } else {
    callback()
  }
}
const validatePass = (rule: any, value: any, callback: any) => {
  if (value.length < 3) {
    callback(new Error('密码不能小于3位'))
  } else {
    callback()
  }
}
const loginForm = ref({
  username: Cookies.get('username') || '',
  password: Cookies.get('password') || '',
})
const loginRules = ref({
  username: [{ required: true, trigger: 'blur', validator: validateUsername }],
  password: [{ required: true, trigger: 'blur', validator: validatePass }],
})
const loading = ref(false)
const pwdType = ref('password')
const ruleFormRef = ref<FormInstance>()

const showPwd = () => {
  if (pwdType.value === 'password') {
    pwdType.value = ''
  } else {
    pwdType.value = 'password'
  }
}
const handleLogin = async (formEl: FormInstance | undefined) => {
  await formEl?.validate()
  loading.value = true
  try {
    await store.Login(loginForm.value)
    loading.value = false
    Cookies.set('username', loginForm.value.username || '', { expires: 15 })
    Cookies.set('password', loginForm.value.password || '', { expires: 15 })
    const path = route.query.redirect as string
    router.push({ path: path ? path : '/' })
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-form-layout {
  position: absolute;
  left: 0;
  right: 0;
  width: 360px;
  margin: 140px auto;
  border-top: 10px solid #409eff;
}

.login-title {
  text-align: center;
}
</style>
